package com.bd.rj.backstage.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bd.rj.backstage.service.IEnterpriseService;
import com.bd.rj.backstage.service.IInterviewService;
import com.bd.rj.common.entity.Interview;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/interview")
public class InterviewController {
    @Resource
    IInterviewService interviewService;
    @Resource
    IEnterpriseService enterpriseService;
    /**
     * 查询老师接收到的简历投递信息
     * @author 白世俊
     * @date 2025/1/7
     */
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public Map<String, Object> queryCondition(
            @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
            @RequestParam(value = "pagesize", defaultValue = "10") int pageSize,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "status",required = false) Integer status) {
        long userId = StpUtil.getLoginIdAsLong();
        IPage<Map> interviewByPageAndCondition = interviewService.getInterviewByPageAndCondition(pagenum, pageSize,userId,name,status);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", interviewByPageAndCondition);
        return map;
    }
    /**
     * @desc 删除简历
     * @author 白世俊
     * @date 2025/1/8
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Map<String,Object> deleteInterview(@RequestParam(value = "id") Long id) {
        boolean b = interviewService.removeById(id);
        long userId = StpUtil.getLoginIdAsLong();
         Map<String, Object> map = new HashMap<>();
                 map.put("code", 200);
                 map.put("msg", "删除成功");
        map.put("data", interviewService.getInterviewByPageAndCondition(1,10,userId,null,null));
        return map;
    }
    /**
     * @desc 对企业进行审核,并更新审核记录
     * @author 白世俊
     * @date 2025/1/7
     */
    @RequestMapping(value = "/dostatus", method = RequestMethod.PUT)
    public Map<String, Object> doStatus(@RequestParam(value = "iid" ) long iid,@RequestParam(value = "status") int status) {
        long userId = StpUtil.getLoginIdAsLong();
        QueryWrapper<Interview> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", iid);
        Interview interview = new Interview();
        interview.setStatus(status);
        interviewService.update(interview,queryWrapper);
        //构造审核记录信息
        Map<String, Object> recordInfo = new HashMap<>();
        recordInfo.put("uid", userId);
        recordInfo.put("iid", iid);
        recordInfo.put("status", status);
        //插入审核记录
        interviewService.insertInterviewRecord(recordInfo);
         Map<String, Object> map = new HashMap<>();
                 map.put("code", 200);
                 map.put("msg", "审核成功");
                 map.put("data", status);

        return map;
    }

}
